<style lang="less" scoped>
.contentbox{
  padding-top: 92px;
  height: 100vh;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .navbox{
    display: flex;
    font-size: 26px;
    color: #999;
    height: 79px;
    background: #242A36;
    .nav{
      height: 79px;
      box-sizing: border-box;
      border-bottom: 4px solid rgba(0, 0, 0, 0);
      line-height: 79px;
      padding: 0 38px;
    }
    .active{
      color: #fff;
      border-color: #fff;
    }
  }
  .pack{
    padding: 28px 20px;
    flex: 1;
    overflow-y: auto;
    .total{
      display: flex;
      justify-content: space-between;
      font-size: 20px;
      color: #666;
      span{
        margin-right: 50px;
      }
    }
    .sortbox{
      margin: 19px 0;
      font-size: 20px;
      color: #999;
      display: flex;
      justify-content: space-between;
      align-content: center;
      .sort{
        display: flex;
        align-items: center;
        .sortitem{
          width: 172px;
          height: 49px;
          background: #242A36;
          text-align: center;
          line-height: 49px;
          margin-right: 12px;
        }
        .active{
          color: #fff
        }
        .allbox{
          width: 28px;
          height: 27px;
          border: 1px solid #999999;
          border-radius: 50%;
          padding: 4px;
          box-sizing: border-box;
          margin-right: 6px;
          .allchecked{
            height: 100%;
            background: #FFA22A;
            border-radius: 50%;
          }
        }
      }
    }
    .nodata{
      padding: 80px 0;
      text-align: center;
      font-size: 26px;
      color: #666;
      img{
        width: 195px;
        margin-bottom: 23px;
      }
    }
    .listcontent{
      display: flex;
      flex-wrap: wrap;
      padding: 25px 15px 0;
      .dataitem{
        padding: 0 8px 30px;
        width: 50%;
        box-sizing: border-box;
        .color1{
          background-image: url(../../assets/images/color1.png);
        }
        .color0{
          background-image: url(../../assets/images/color0.png);
        }
        .color2{
          background-image: url(../../assets/images/color2.png);
        }
        .color3{
          background-image: url(../../assets/images/color3.png);
        }
        .color4{
          background-image: url(../../assets/images/color4.png);
        }
        .gun{
          height: 230px;
          background-size: 100% 100%;
          position: relative;
          margin-bottom: 15px;
          display: flex;
          align-items: center;
          justify-content: space-around;
          .newleval{
            position: absolute;
            left: 0;
            top: 0;
            display: inline-block;
            height: 39px;
            padding: 0 12px;
            font-size: 18px;
            color: #fff;
            font-weight: bold;
            line-height: 39px;
          }
          img{
            width: 80%;
          }
          .checked{
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            transition: all .2s;
            opacity: 0;
          }
          .skinname{
            position: absolute;
            left: 20px;
            bottom: 12px;
            font-size: 22px;
            color: #fff;
            white-space:nowrap;
            overflow:hidden;
            text-overflow:ellipsis;
            width: 90%;
          }
        }
      }
    }
  }
  .recordbox{
    flex: 1;
    overflow-y: auto;
    padding: 22px 20px;
    font-size: 20px;
    color: #999;
    .tablerow{
      display: flex;
      align-items: center;
      min-height: 62px;
      border: 1px solid #333;
      border-top: none;
      text-align: center;
      div{
       flex: 1;
      }
    }
    .tabletitle{
      border-top: 1px solid #333;
      color: #666;
    }
  }
}
</style>

<template>
<div>
  <top-bar></top-bar>
  <div class="contentbox">
    <div class="navbox">
      <div v-for="(nav, index) in pageData.navList" :key="index" :class="['nav', {active: nav.active}]" @click="checkNav(nav)">{{nav.name}}</div>
    </div>
    <div v-if="pageData.navList[0].active" @scroll="scorllpack" class="pack">
      <div class="total">
        <div>件数: 0 <span></span>估值：0</div>
        <div>多次拒绝收货将无法取回!</div>
      </div>
      <div class="sortbox">
        <div class="sort">
          <div v-for="(sort, index) in pageData.sortList" :key="index" :class="['sortitem', {active: index === currentSortIndex}]" @click="checkSort(index)">{{sort}}</div>
        </div>
        <div class="sort" @click="changeCheckState">
          <div class="allbox"><div :class="[{ allchecked: checkAll }]"></div></div>
        全选</div>
      </div>
      <div v-if="pageData.packList.length" class="listcontent">
        <div v-for="dataitem in pageData.packList" :key="dataitem.id" class="dataitem">
          <div class="gun" :class="`color${1}`" @click="dataitem.checked = !dataitem.checked">
            <p class="newleval">磨损度</p>
            <img :src="dataitem.skinDetailImage" alt="">
            <p class="skinname">{{dataitem.skinDetailName}}</p>
            <img class="checked" :style="{opacity: dataitem.checked ? '1' : '0'}" src="../../assets/images/checked.png" alt="">
          </div>
        </div>
      </div>
      <div v-else class="nodata">
        <img src="../../assets/images/nodata.png" alt="">
        <p>暂无数据</p>
      </div>
    </div>
    <div v-if="pageData.navList[1].active" class="recordbox">
      <div class="tabletitle tablerow">
        <div>时间</div>
        <div>道具名称</div>
        <div>订单号</div>
      </div>
      <div class="tablerow btn">
        <div>时间</div>
        <div>道具名称</div>
        <div>订单号</div>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import { backpackList } from '../../api/home.js'
import { defineComponent, provide, reactive, ref, onMounted } from "@vue/runtime-core"
import { Toast } from 'vant'
export default defineComponent({
  setup() {
    provide('title', '背包')
    const pageData = reactive({
      navList: [
        {
          name: '背包',
          active: true
        },
        {
          name: '取回记录',
          active: false
        }
      ],
      sortList: [ '按获得时间排序', '价格高低排序','价格低高排序' ],
      packList: []
    })
    let currentSortIndex = ref(0)
    let totalPage = 0
    let checkAll = ref(false)

    let page = 1
    const checkNav = (nav) => {
      pageData.navList.forEach(item => item.active = false)
      nav.active = true
    }

    const checkSort = (index) => {
      currentSortIndex.value = index
      page = 1
      getDataList()
    }

    const changeCheckState = () => {
      checkAll.value = !checkAll.value
      pageData.packList.forEach(item => item.checked = checkAll.value)
    }

    const getDataList = async () => {
      const query = {
        pageNum: page,
        pageSize: 10,
        sort: currentSortIndex.value
      }
      const loading = Toast.loading('加载中')
      const res = await backpackList(query)
      loading.clear()
      if (res.code === 200) {
        totalPage = res.data.totalPages
        res.data.content.forEach(item => item.checked = false)
        if (page === 1) {
          pageData.packList = res.data.content
        } else {
          pageData.packList = [ ... pageData.packList, ... res.data.content ]
        }
      }
    }

    const scorllpack = (e) => {
      if (e.target.scrollTop + e.target.clientHeight >= e.target.scrollHeight) {
        if (page < totalPage) {
          page += 1
          getDataList()
        }
      }
    }

    onMounted(() => {
      getDataList()
    })

    return {
      changeCheckState,
      pageData,
      checkNav,
      checkSort,
      checkAll,
      currentSortIndex,
      getDataList,
      scorllpack
    }
  }
})
</script>